
#include "../adt/headers/queue.h"
#include "headers/task.h"
#include "../api/headers/string.h"
#include "headers/timer.h"

resource_check_t res_check[N_OF_RES_TYPES];
static BOOL queue_not_empty(u32int queue);

void initialise_resource_management()
{
	memset(res_check, 0, N_OF_RES_TYPES * sizeof(resource_check_t));
	res_check[RES_QUEUE] = queue_not_empty;
	//Register all resource types checkers.
}

void block_process(task_t* task, u32int res_id, u32int param)
{
	asm volatile ("cli");
	if(!res_check[res_id](param))
	{
		task->required_resource.id = res_id;
		task->required_resource.param = param;
		task->state = BLOCKED;
		raise_timer_int(); //Call switcher (scheduler)
		task->state = READY;
	}
	else
	{
		task->state = READY;
	}
	asm volatile ("sti");
	return;
}

BOOL resource_is_available(resource_type* res)
{
	return res_check?res_checks[res->id](res->param):true;
}

static BOOL queue_not_empty(u32int queue)
{
	return !queue_is_empty((queue_type*)queue);
}